package com.ellecity06.common.helper

import android.os.SystemClock

/**
 * @author ellecity06
 * @time 2024/11/22 9:39
 * @des
 */
class ClickHelper {
    companion object {

        /** 数组的长度为2代表只记录双击操作 */
        private val TIME_ARRAY: LongArray = LongArray(2)

        /**
         * 是否在短时间内进行了双击操作
         */
        fun isOnDoubleClick(time: Int = 1000): Boolean {
            System.arraycopy(TIME_ARRAY, 1, TIME_ARRAY, 0, TIME_ARRAY.size - 1)
            TIME_ARRAY[TIME_ARRAY.size - 1] = SystemClock.uptimeMillis()
            return TIME_ARRAY[0] >= (SystemClock.uptimeMillis() - time)
        }

        /**
         * 节流点击
         */
        fun debounceClick(block:()->Unit){
            if (!isOnDoubleClick(500)){
                block.invoke()
            }
        }
    }
}